package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fourth.dao.ZoneDao;
import com.ljx.entity.Zone;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author：lmy
 * @Date：2023/2/28 14:41
 */
@RestController
@RequestMapping("home/api/zone")
public class ApiZoneController {
    @Autowired(required = false)
    private ZoneDao zoneDao;

    @PostMapping("fuzzySearch/{currentPage}/{pageSize}")
    public Result fuzzySearch(@PathVariable Integer currentPage, @PathVariable Integer pageSize,
                              @RequestBody Zone zone){
        Page<Zone> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        if (zone.getZoneName() != null){
            wrapper.like("zone_name",zone.getZoneName());
        }
        if (zone.getIsDisable() != null){
            wrapper.like("is_disable",zone.getIsDisable());
        }
        Page<Zone> zonePage = zoneDao.selectPage(page,wrapper);
        return new Result<>(2000,"查询成功",zonePage);
    }

    @PostMapping("updateDisable/{id}/{isDisable}")
    public Result updateDisable(@PathVariable String id,@PathVariable Integer isDisable){
        Zone zone = new Zone();
        zone.setId(id);
        zone.setIsDisable(isDisable);
        int row = zoneDao.updateById(zone);
        if (row ==1 ){
            return new Result(2000,"修改状态成功");
        }
        return new Result(2000,"修改状态失败");
    }

    @PostMapping("insertPrefecture")
    public Result insertPrefecture(@RequestBody Zone zone){
        int row = zoneDao.insert(zone);
        if (row == 1){
            return new Result(2000,"添加成功");
        }
        return new Result(5000,"添加失败");
    }

    @PostMapping("updatePrefecture")
    public Result updatePrefecture(@RequestBody Zone zone){
        int row = zoneDao.updateById(zone);
        if (row == 1){
            return new Result(2000,"修改成功");
        }
        return new Result(5000,"修改失败");
    }

    @GetMapping("removePrefecture/{id}")
    public Result removePrefecture(@PathVariable String id){
        int row = zoneDao.deleteById(id);
        if (row == 1){
            return new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");
    }
}
